/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package gov.decea.projects.sgq.dao;

import gov.decea.projects.sgq.exception.DAOException;
import gov.decea.projects.sgq.model.MetarGroupData;
import gov.decea.projects.sgq.model.MeteorologicalReportGroupData;
import gov.decea.projects.sgq.model.TafGroupData;
import gov.decea.projects.sgq.model.Usuario;
import gov.decea.projects.sgq.util.MessageHelper;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;

/**
 *
 * @author fernandofot
 */
public class HibernateMeteorologicaReportGroupDataDAO implements MeteorologicalReportGroupDataDAO {

    private SessionFactory sessionFactory;

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    @Override
    public Long create(MeteorologicalReportGroupData groupData) throws DAOException {
        Session session = sessionFactory.getCurrentSession();

        try {
            Long id = (Long) session.save(groupData);

            return id;
        } catch (HibernateException e) {

            Logger.getLogger(HibernateMeteorologicaReportGroupDataDAO.class.getName()).log(
                    Level.SEVERE, null, e);
            throw new DAOException(MessageHelper.getMessage("groupData.create.error"));

        }
    }

    @Override
    public void update(MeteorologicalReportGroupData groupData) throws DAOException {
        Session session = sessionFactory.getCurrentSession();

        try {
            session.update(groupData);

        } catch (HibernateException e) {

            Logger.getLogger(HibernateMeteorologicaReportGroupDataDAO.class.getName()).log(
                    Level.SEVERE, null, e);
            throw new DAOException(MessageHelper.getMessage("groupData.update.error"));

        }
    }

    @Override
    public void remove(Long id) throws DAOException {
        Session session = sessionFactory.getCurrentSession();

        try {
            MeteorologicalReportGroupData groupData
                    = (MeteorologicalReportGroupData) session.get(MeteorologicalReportGroupData.class, id);
            session.delete(groupData);

        } catch (HibernateException e) {

            Logger.getLogger(HibernateMeteorologicaReportGroupDataDAO.class.getName()).log(
                    Level.SEVERE, null, e);
            throw new DAOException(MessageHelper.getMessage("groupData.remove.error"));

        }
    }

    @Override
    public MeteorologicalReportGroupData find(Long id) throws DAOException {
        Session session = sessionFactory.getCurrentSession();

        try {
            MeteorologicalReportGroupData groupData = (MeteorologicalReportGroupData) session.get(Usuario.class, id);
            return groupData;

        } catch (HibernateException e) {

            Logger.getLogger(HibernateMeteorologicaReportGroupDataDAO.class.getName()).log(
                    Level.SEVERE, null, e);
            throw new DAOException(MessageHelper.getMessage("groupData.find.unique.error"));

        }
    }

    @Override
    public List<MetarGroupData> findAllMetar(Long organizacaoId, Long aerodromoId, Date periodo) throws DAOException {
        Session session = sessionFactory.getCurrentSession();
        SimpleDateFormat df = new SimpleDateFormat("mm-yyyy");

        try {
            Criteria criteria = session.createCriteria(MetarGroupData.class);

            if (organizacaoId != null) {
                criteria = criteria.add(Restrictions.eq("aerodromo.organizacaoId", organizacaoId));
            }

            if (aerodromoId != null) {
                criteria = criteria.add(Restrictions.eq("aerodromo.id", aerodromoId));
            }
            if (periodo != null) {
                Date periodoFormatted = periodo;
                try {
                    periodoFormatted = df.parse(df.format(periodo));
                } catch (ParseException ex) {
                    Logger.getLogger(HibernateMeteorologicaReportGroupDataDAO.class.getName()).log(Level.SEVERE, null, ex);
                }
                criteria = criteria.add(Restrictions.ilike("codigo", periodoFormatted));
            }
            List<MetarGroupData> dataList = criteria.list();

            return dataList;

        } catch (HibernateException e) {

            Logger.getLogger(HibernateMeteorologicaReportGroupDataDAO.class.getName()).log(
                    Level.SEVERE, null, e);
            throw new DAOException(MessageHelper.getMessage("groupData.find.list.error"));

        }
    }

    @Override
    public List<TafGroupData> findAllTaf(List<Long> organizacoesIds, List<Long> aerodromosIds, List<Date> periodos) throws DAOException {
        Session session = sessionFactory.getCurrentSession();
        SimpleDateFormat df = new SimpleDateFormat("mm-yyyy");

        try {
            Criteria criteria = session.createCriteria(TafGroupData.class);

            List<TafGroupData> dataList = criteria.list();

            return dataList;

        } catch (HibernateException e) {

            Logger.getLogger(HibernateMeteorologicaReportGroupDataDAO.class.getName()).log(
                    Level.SEVERE, null, e);
            throw new DAOException(MessageHelper.getMessage("groupData.find.list.error"));

        }
    }

    @Override
    public List<TafGroupData> findAllTaf(Long organizacaoId, Long aerodromoId, Date periodo) throws DAOException {
        Session session = sessionFactory.getCurrentSession();
        SimpleDateFormat df = new SimpleDateFormat("mm-yyyy");

        try {
            Criteria criteria = session.createCriteria(TafGroupData.class);

            if (organizacaoId != null) {
                criteria = criteria.add(Restrictions.eq("aerodromo.organizacaoId", organizacaoId));
            }

            if (aerodromoId != null) {
                criteria = criteria.add(Restrictions.eq("aerodromo.id", aerodromoId));
            }
            if (periodo != null) {
                Date periodoFormatted = periodo;
                try {
                    periodoFormatted = df.parse(df.format(periodo));
                } catch (ParseException ex) {
                    Logger.getLogger(HibernateMeteorologicaReportGroupDataDAO.class.getName()).log(Level.SEVERE, null, ex);
                }
                criteria = criteria.add(Restrictions.ilike("codigo", periodoFormatted));
            }
            List<TafGroupData> dataList = criteria.list();

            return dataList;

        } catch (HibernateException e) {

            Logger.getLogger(HibernateMeteorologicaReportGroupDataDAO.class.getName()).log(
                    Level.SEVERE, null, e);
            throw new DAOException(MessageHelper.getMessage("groupData.find.list.error"));

        }
    }

    @Override
    public List<MetarGroupData> findAllMetar(List<Long> organizacoesIds, List<Long> aerodromosIds, List<Date> periodos) throws DAOException {
        Session session = sessionFactory.getCurrentSession();
        SimpleDateFormat df = new SimpleDateFormat("mm-yyyy");

        try {
            Criteria criteria = session.createCriteria(MetarGroupData.class);

            List<MetarGroupData> dataList = criteria.list();

            return dataList;

        } catch (HibernateException e) {

            Logger.getLogger(HibernateMeteorologicaReportGroupDataDAO.class.getName()).log(
                    Level.SEVERE, null, e);
            throw new DAOException(MessageHelper.getMessage("groupData.find.list.error"));

        }
    }
}
